package com.city.api;

import com.city.config.Nginx;
import com.city.domain.pub.PubFiletype;
import com.city.domain.pub.PubFilepath;
import com.city.service.pub.PubFiletypeService;
import com.city.service.pub.SaveService;
import com.city.helper.FileUploadHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @deprecation: 多文件上传
 * @dmail: 398521613@qq.com
 * @author: L-H
 * @date: 2018-07-23 18:27
 */
@RestController
@RequestMapping("/api/spfile")
public class SpFileUpload {

    @Autowired
    private SaveService saveService;

    @Autowired
    private PubFiletypeService filetypeService;

    @PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> file(
            MultipartFile file,//文件
            @RequestParam(value = "tableName") String tableName,//表名
            @RequestParam(value = "index") String index    //关联字段值
    ) throws IOException {

        Map<String,Object> map = new HashMap<>();

        if(null != file){
            String localFileName = file.getOriginalFilename();
            //获取文件后缀名
            String fileTyle=localFileName.substring(localFileName.lastIndexOf("."),localFileName.length());

            PubFiletype pubFiletype = filetypeService.getFileTypeByName(fileTyle);


            if(null == pubFiletype){
                map.put("code",-1);
                map.put("url","");
                map.put("msg","文件类型不符合要求");        //提示消息
                return map;
            }else {
                String fileName = FileUploadHelper.getRandomString(35) + '.' + localFileName.substring(localFileName.lastIndexOf(".") + 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = simpleDateFormat.format(System.currentTimeMillis());
//            String staticPath = ResourceUtils.getURL(Nginx.URL + Upload.IMG_URL).getPath();
                String staticPath = "";
                if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){
                    staticPath = Nginx.ABS_URL;
                }else{
                    staticPath = Nginx.WIN_ABS_URL;
                }
                String savePath= staticPath + "/" + date + "/";
                //创建保存路径
                File fileDir=new File(savePath);
                if(!fileDir.exists()){
                    fileDir.mkdirs();
                }
                String path = savePath + fileName;
                File localFile = new File(path);

                file.transferTo(localFile);
                Map<String,Object> map2 = new HashMap<>();

                String url = "/" + date + "/" + fileName;
                String src = Nginx.URL + url;

                //保存到数据库
                PubFilepath filepath = new PubFilepath();
                filepath.setTableName(tableName);
                filepath.setIdIndex(index);
                filepath.setFilePath(src);
                filepath.setTypeId(pubFiletype.getId());
                saveService.save(filepath);

                map.put("code",0);
                map.put("url",url);
                map.put("fileName",fileName);
                map.put("msg","上传成功");        //提示消息
                map.put("data",map2);
                map2.put("src",src);            //图片url
                map2.put("title","");           //图片名称，这个会显示在输入框里
                return map;
            }
        }
        return null;
    }


    /**
     * @author  王宇
     * @date 2018/9/6
     * @description
     */

    @PostMapping("/images")
    @ResponseBody
    public Map<String,Object> images(
            MultipartFile file  //关联字段值
    ) throws IOException {

        Map<String,Object> map = new HashMap<>();

        if(null != file){
            String localFileName = file.getOriginalFilename();
            //获取文件后缀名
            String fileTyle=localFileName.substring(localFileName.lastIndexOf("."),localFileName.length());

            PubFiletype pubFiletype = filetypeService.getFileTypeByName(fileTyle);


            if(null == pubFiletype){
                map.put("code",-1);
                map.put("url","");
                map.put("msg","文件类型不符合要求");        //提示消息
                return map;
            }else {
                String fileName = FileUploadHelper.getRandomString(35) + '.' + localFileName.substring(localFileName.lastIndexOf(".") + 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = simpleDateFormat.format(System.currentTimeMillis());
//            String staticPath = ResourceUtils.getURL(Nginx.URL + Upload.IMG_URL).getPath();
                String staticPath =  Nginx.ABS_URL;
                String savePath= staticPath + "/" + date + "/";
                //创建保存路径
                File fileDir=new File(savePath);
                if(!fileDir.exists()){
                    fileDir.mkdirs();
                }
                String path = savePath + fileName;
                File localFile = new File(path);

                file.transferTo(localFile);
                Map<String,Object> map2 = new HashMap<>();

                String url = "/" + date + "/" + fileName;
                String src = Nginx.URL + url;

                String location = src;
                //保存到数据库
                PubFilepath filepath = new PubFilepath();

                filepath.setTableName("文章表");
                long time = new Date().getTime();
                String s = String.valueOf(time);
                filepath.setIdIndex(s);
                filepath.setFilePath(src);
                filepath.setTypeId(pubFiletype.getId());
                saveService.save(filepath);

                map.put("code",0);
                map.put("url",url);
                map.put("fileName",fileName);
                map.put("msg","上传成功");        //提示消息
                map.put("data",map2);
                map2.put("src",src);            //图片url
                map2.put("title","");           //图片名称，这个会显示在输入框里
                return map;
            }
        }
        return null;
    }
}
